package com.lanhai.javaBean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.lanhai.model.Station;
import com.mybatis.dao.StationDao;

public class StationBean {

	/**
	 * 地图节点列表
	 */
	public static List<Station> loadData(SqlSessionFactory sqlSessionFactory){
		List<Station> stationList = new ArrayList<Station>();
		SqlSession sqlSession = sqlSessionFactory.openSession();
		try {
			StationDao dao = sqlSession.getMapper(StationDao.class);
			List<Map<String, Object>> list = dao.selectAll();
			if(list.size() > 0){
				for(Map<String, Object> m:list){
					Station station = new Station();
					station.setId((Integer)m.get("id"));
					station.setName((String)m.get("name"));//名称
					station.setSname((String)m.get("sname"));
					station.setInNum((Integer)m.get("inNum"));
					station.setType((Integer)m.get("type"));
					String cdIds = (String)m.get("controlAreaIds");//对应交管区ID
					if(cdIds != null){
						String[] cdIdArr = cdIds.split(",");
						if(cdIdArr.length > 0){
							station.setControlAreaIds(cdIdArr);
						}
					}
					station.setStep((Integer)m.get("step"));
//					station.setNextStep((Integer)m.get("nextStep"));
					station.setIpAddress((String)m.get("ipAddress"));
					station.setProtocol((String)m.get("protocol"));
//					station.setReadTar((Integer)m.get("readTar"));
//					station.setWriteTar((Integer)m.get("writeTar"));
					station.setPort((Integer)m.get("port"));
					station.setPGroup((Integer)m.get("pGroup"));
					station.setPType((Integer)m.get("pType"));
					station.setState((Integer)m.get("state"));
					station.setIsClear((Integer)m.get("isClear"));
					station.setAgentIpAddress((String)m.get("agentIpAddress"));
					station.setAgentPort((Integer)m.get("agentPort"));
					station.setAgentId((Integer)m.get("agentId"));
					station.setAgentLocalId((Integer)m.get("agentLocalId"));
					station.setLimit((Integer)m.get("limit"));
					station.setInitFlag(false);
					stationList.add(station);					
				}
			}
	    }catch(Exception e){
	    	e.printStackTrace();
	    }finally{
	    	sqlSession.close();
	    }
        return stationList;
	}	
	
	public static List<Map<String, Object>> loadTempData(SqlSessionFactory sqlSessionFactory){
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		SqlSession sqlSession = sqlSessionFactory.openSession();
		try {
			StationDao dao = sqlSession.getMapper(StationDao.class);
			list = dao.selectTempData();
	    }catch(Exception e){
	    	e.printStackTrace();
	    }finally{
	    	sqlSession.close();
	    }
		return list;
	}	
	
	/**
	 * 对列表以序号从大到小进行排序
	 * @param parkList
	 * @return
	 */
	public static List<Station> IdSort(List<Station> stationList){
        Collections.sort(stationList, new Comparator<Object>() {
            public int compare(Object a, Object b) {
                int one = ((Station) a).getId();
                int two = ((Station) b).getId();
                return one - two;//升序
                //return two - one;//降序
            }
        });	
        return stationList;
	}
	
}
